from keras import models, layers
from tensorflow.keras.utils import plot_model
from tensorflow.keras.applications import VGG16
from tensorflow import optimizers
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import recall_score
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
from sklearn.metrics import confusion_matrix

#训练样本的目录
train_dir='./train_dataset/train/train'
#验证样本的目录
validation_dir='./train_dataset/train/validation'

#训练集生成器---训练集数据加强
train_datagen=ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

train_generator=train_datagen.flow_from_directory(
    directory=train_dir,
    target_size=(150,150),
    class_mode='binary',
    batch_size=20
)

#验证样本生成器
validation_datagen=ImageDataGenerator(rescale=1./255)
validation_generator=validation_datagen.flow_from_directory(
    directory=validation_dir,
    target_size=(150,150),
    class_mode='binary',
    batch_size=20
)

print(train_generator.class_indices)
print(validation_generator.class_indices)
#载入模型
model=load_model('model_data/model4_2_VGG 16_cats_vs_dogs_1.h5')
y_pred = model.predict(validation_generator)
print(y_pred)
print(((y_pred)>0.5).astype("int32"))
print(y_pred.shape)

y_pred=[(int)((y_pred[i][0] + 0.5) / 1.0) for i in range(len(y_pred))]
y_pred =np.asarray(y_pred).astype('float32')

print(validation_generator.labels)
# #召回率
recall = recall_score(validation_generator.labels, y_pred)
print(recall)
#分类报告
from sklearn.metrics import classification_report
print("分类报告:\n",classification_report(validation_generator.labels, y_pred))
print("混淆矩阵:\n",confusion_matrix(validation_generator.labels, y_pred))